package com.white.algebra.dao.impl;

import com.white.algebra.dao.DiscussDao;
import com.white.algebra.domain.Discuss;
import com.white.algebra.domain.Discussion;
import com.white.algebra.domain.Role;
import com.white.algebra.domain.Syslog;
import com.white.algebra.utils.JdbcUtil;
import com.white.algebra.utils.JedisPoolUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DiscussDaoImpl implements DiscussDao {
    @Override
    public List<Discussion> findAll(int startRecord, int size,String name) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        List<Discussion> discussionList=null;
        if(name!=null&& !"".equals(name)){
            String sql = "select * from discussion where name like concat('%',?,'%') limit ?,?";
            Object[] params = { name,startRecord,size};
            discussionList= (List<Discussion>) qr.query(sql,params, new BeanListHandler(Discussion.class));
        }else {
            String sql = "select * from discussion limit ?,?";
            Object[] params = {startRecord,size};
            discussionList= (List<Discussion>) qr.query(sql,params, new BeanListHandler(Discussion.class));
        }
        return discussionList;
    }

    @Override
    public List<Discuss> findAllDiscuss() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        List<Discuss> discussList=null;
        String sql= "select id,thumbsUp from discuss";
        discussList= (List<Discuss>) qr.query(sql, new BeanListHandler(Discuss.class));
        return discussList;
    }

    @Override
    public void insertDiscussion(Discussion discussion) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "INSERT INTO discussion (`author`,`name`, `text`,`time`) VALUES (?, ?, ?,?)";
        Object[] params = {discussion.getAuthor(),discussion.getName(),discussion.getText(),discussion.getTime()};
        qr.update(sql, params);
    }

    @Override
    public Discussion findDiscussionById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        Discussion discussion=null;
        String sql = "select * from discussion where id =?";
        Object[] params = { id};
        discussion= (Discussion) qr.query(sql,params, new BeanHandler(Discussion.class));
        return discussion;
    }

    /**
     * 通过帖子id查找评论
     *
     * @param id
     * @param startRecord
     * @param size
     * @return
     */
    @Override
    public List<Discuss> findDiscussByDiscussionId(String id, int startRecord, int size) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        List<Discuss> discussList=null;
        String sql = "select * from discuss where discussionId =?";
        Object[] params = { id};
        discussList= (List<Discuss>) qr.query(sql,params, new BeanListHandler(Discuss.class));
        return discussList;
    }

    /**
     * 通过ID查找评论
     *
     * @param id
     * @return
     */
    @Override
    public Discuss findDiscussById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        Discuss discuss=null;
        String sql = "select * from discuss where id =?";
        Object[] params = { id};
        discuss= (Discuss) qr.query(sql,params, new BeanHandler(Discuss.class));
        return discuss;
    }

    @Override
    public int getDiscussionNum(String name) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        int num=0;
        if(name!=null&& !name.equals("")){
            String sql = "select count(*) from discussion where name like concat('%',?,'%')";
            Object[] params = { name};
            num= ((Long) qr.query(sql,params, new ScalarHandler(1))).intValue();
        }else {
            String sql = "select count(*) from discussion";
            num= ((Long) qr.query(sql, new ScalarHandler(1))).intValue();
        }
        return num;
    }

    @Override
    public int getDiscussNum(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        int num=0;
        String sql = "select count(*) from discuss where discussionId =?";
        Object[] params = {id};
        num= ((Long) qr.query(sql,params, new ScalarHandler(1))).intValue();
        return num;
    }

    @Override
    public void updateThumbsUp(String mapKey, String mapValue) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "update discuss set `thumbsUp`=? where id=?";
        Object[] params = {mapValue,mapKey};
        qr.update(sql, params);
    }


    @Override
    public void thumbsUp(String id, String fun) {
        Jedis jedis= JedisPoolUtils.getJedis();
        String thumb=jedis.hget("discuss",id);
        //t 点赞   f 去赞
        if("t".equals(fun)){
            jedis.hset("discuss",id, String.valueOf(Integer.parseInt(thumb)+1));
        }else {
            jedis.hset("discuss",id, String.valueOf(Integer.parseInt(thumb)-1));
        }
        jedis.close();

    }

    @Override
    public void insertDiscuss(Discuss discuss) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "INSERT INTO discuss (`author`,`discussionId`,`discussId`, `text`,`time`) VALUES (?,?, ?, ?,?)";
        Object[] params = {discuss.getAuthor(),discuss.getDiscussionId(),discuss.getDiscussId(),discuss.getText(),discuss.getTime()};
        qr.update(sql, params);

    }

    @Override
    public void viewDiscussion(String discussionId) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "update discussion set `replys`=`replys`+1 where id=?";
        Object[] params = {discussionId};
        qr.update(sql, params);
    }
}
